package cn.eud.hunau.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.eud.hunau.service.MovieService;
import cn.eud.hunau.common.R;
import jakarta.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;

    // 查询所有电影
    @GetMapping("/all")
    public R selectAll(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        return R.ok().data("movies", movieService.selectAll());
    }

    // 根据电影id查询电影
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        return R.ok().data("movie", movieService.selectById(id));
    }

    // 收藏电影
    @PostMapping("/like")
    public R likeMovie(@RequestParam Long movieId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.likeMovie(userId, movieId);
        return success ? R.ok().message("收藏成功") : R.error().message("收藏失败");
    }

    // 取消收藏
    @PostMapping("/unlike")
    public R unlikeMovie(@RequestParam Long movieId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.unlikeMovie(userId, movieId);
        return success ? R.ok().message("取消收藏成功") : R.error().message("取消收藏失败");
    }

    // 查询用户收藏的电影
    @GetMapping("/liked")
    public R getLikedMovies(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        return R.ok().data("movies", movieService.getLikedMovies(userId));
    }

    // 判断用户是否收藏某电影
    @GetMapping("/isLiked")
    public R isMovieLiked(@RequestParam Long movieId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean liked = movieService.isMovieLiked(userId, movieId);
        return R.ok().data("liked", liked);
    }

    // 评分电影
    @PostMapping("/rate")
    public R rateMovie(@RequestParam Long movieId, @RequestParam Integer score, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.rateMovie(userId, movieId, score);
        return success ? R.ok().message("评分成功") : R.error().message("评分失败");
    }

    // 查看用户对某电影的评分
    @GetMapping("/score")
    public R getUserScore(@RequestParam Long movieId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        Integer score = movieService.getUserScore(userId, movieId);
        return R.ok().data("score", score);
    }

    // 查看某电影的所有评分
    @GetMapping("/scores")
    public R getMovieScores(@RequestParam Long movieId) {
        return R.ok().data("scores", movieService.getMovieScores(movieId));
    }

    // 评论电影
    @PostMapping("/comment")
    public R commentMovie(@RequestParam Long movieId, @RequestParam String comment, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.commentMovie(userId, movieId, comment);
        return success ? R.ok().message("评论成功") : R.error().message("评论失败");
    }

    // 查看某电影的所有评论
    @GetMapping("/comments")
    public R getMovieComments(@RequestParam Long movieId) {
        return R.ok().data("comments", movieService.getMovieComments(movieId));
    }

    // 查看用户对某电影的评论
    @GetMapping("/userComments")
    public R getUserComments(@RequestParam Long movieId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        return R.ok().data("comments", movieService.getUserComments(userId, movieId));
    }

    // 修改评论
    @PostMapping("/updateComment")
    public R updateComment(@RequestParam Long commentId, @RequestParam String comment, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.updateComment(commentId, userId, comment);
        return success ? R.ok().message("修改成功") : R.error().message("修改失败");
    }

    // 删除评论
    @PostMapping("/deleteComment")
    public R deleteComment(@RequestParam Long commentId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return R.error().message("未登录");
        }
        boolean success = movieService.deleteComment(commentId, userId);
        return success ? R.ok().message("删除成功") : R.error().message("删除失败");
    }

    // 热门排行展示
    @GetMapping("/hot")
    public R getHotMovies(@RequestParam(defaultValue = "10") int limit) {
        return R.ok().data("movies", movieService.getHotMovies(limit));
    }

    // 多条件综合分类查询电影
    @GetMapping("/search")
    public R searchMovies(@RequestParam(required = false) Long typeId,
                         @RequestParam(required = false) Long regionId,
                         @RequestParam(required = false) Long languageId) {
        return R.ok().data("movies", movieService.getMoviesByConditions(typeId, regionId, languageId));
    }

    // 查询所有类型
    @GetMapping("/types")
    public R getAllTypes() {
        return R.ok().data("types", movieService.getAllTypes());
    }

    // 查询所有地区
    @GetMapping("/regions")
    public R getAllRegions() {
        return R.ok().data("regions", movieService.getAllRegions());
    }

    // 查询所有语言
    @GetMapping("/languages")
    public R getAllLanguages() {
        return R.ok().data("languages", movieService.getAllLanguages());
    }
}